package com.lunker.moneyrecord;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Lock extends Activity implements OnClickListener {
	
	public static final int RESULT_PASSWORD_IS_CORRECT = 2;
	public static final int RESULT_PASSWORD_IS_INCORRECT = 3;
	private SharedPreferences config = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lock);
		((Button)findViewById(R.id.btn_lock_comfirm)).setOnClickListener(this);
		config = getSharedPreferences("Settings", Context.MODE_PRIVATE);
	}
	
	@Override
	public void onClick(View v) {		
		
		String storedPassword = config.getString("password", null);
		String enteredPassword = ((EditText) findViewById(R.id.edittext_lock_password))
				.getText().toString();

		if (storedPassword == null) {
			//No password been set. Let user in.
			setResult(RESULT_PASSWORD_IS_CORRECT);
			finish();
		} else if (enteredPassword != null && enteredPassword.equals(storedPassword)) {
			//Entered password matches stored password. Let user in.
			setResult(RESULT_PASSWORD_IS_CORRECT);
			finish();
		} else {
			//Wrong password.
			Toast.makeText(this, getString(R.string.password_is_incorrect), 
					Toast.LENGTH_SHORT).show();
		}
		
	}
	
}
